package com.jixuian.rabbitmq.service;

import com.jixuian.rabbitmq.dao.TransMessageDao;
import com.jixuian.rabbitmq.enumeration.TransMessageType;
import com.jixuian.rabbitmq.po.TransMessagePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class TransaMessageServiceImpl implements TransMessageService {

    @Autowired
    private TransMessageDao transMessageDao;
    @Value("${mq.service}")
    private String serviceName;


    @Override
    public TransMessagePO messageSendReady(String exchange, String routingKey, String body) {
        String id = UUID.randomUUID().toString();
        TransMessagePO po = new TransMessagePO();
        po.setId(id);
        po.setService(serviceName);
        po.setExchange(exchange);
        po.setRoutingKey(routingKey);
        po.setPayload(body);
        po.setDate(new Date());
        po.setSequence(0);
        po.setType(TransMessageType.SEND);
        transMessageDao.insert(po);
        return po;
    }

    @Override
    public void messageSendSuccess(String id) {
        transMessageDao.delete(id, serviceName);
    }

    @Override
    public TransMessagePO messageSendReturn(String id, String exchange, String routingKey, String body) {
        return messageSendReady(exchange, routingKey, body);
    }

    @Override
    public List<TransMessagePO> listReadyMessages() {
        return transMessageDao.selectByTypeAndService(TransMessageType.SEND.toString(), serviceName);
    }

    @Override
    public void messageResend(String id) {
        TransMessagePO po = transMessageDao.selectByIdAndService(id, serviceName);
        po.setSequence(po.getSequence() + 1);
        transMessageDao.update(po);
    }

    @Override
    public void messageDead(String id) {
        TransMessagePO po = transMessageDao.selectByIdAndService(id, serviceName);
        po.setType(TransMessageType.READ);
        transMessageDao.update(po);
    }

    @Override
    public TransMessagePO messageRecieveReady(String id, String exchange, String routingKey, String queue, String body) {
        return null;
    }
}
